<template>
	<view>
		<view class="flex top-box">
			<view class="top-now">
				<img class="img-sty" src="../../static/img/Group_4@2x.png" alt="">
			</view>
			<view class="top-name">{{name || items.distribution_user || items.create_name}}</view>
			<view class="top-res">(招聘专员)</view>
		</view>
		<view class="flex sty" v-for="(list,index) in nameList" :key="index">
			<view>{{list.name}} (招聘专员)</view>
			<view class="btn" @tap="handleClickAllot(list)">分配给TA</view>
		</view>
		<view class="cu-modal show" v-if="showConfirm">
		    <view class="cu-dialog dialog-container">
		        <view class="cu-bar dialog-title-view flex rcb">
		            <view class="dialog-title-style">温馨提示</view>
		            <view class="dialog-btn-close" @tap="onPressCancel">
		                <text class="cuIcon-close"></text>
		            </view>
		        </view>
		        <view class="dialog-content-view flex ccc">
		            <view class="dialog-context flex ccc">{{item_content}}</view>
		        </view>
		        <view class="cu-bar dialog-btn-wrap flex rcb">
		            <view class="btn-item btn-cancel-item flex ccc" @tap="onPressCancel">取消</view>
		            <view class="btn-item btn-submit-item flex ccc" @tap="onPressSubmit">确定</view>
		        </view>
		    </view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showConfirm: false,
				name: '',
				token: '',
				item_list: '',
				item_content: '',
				items: {}, //上个页面传过来的数据
				nameList: [], //后台请求得数据
			}
		},
		onLoad(option) { //option为object类型，会序列化上个页面传递的参数
			// console.log(option.item); //打印出上个页面传递的参数。
			let item = JSON.parse(option.item)
			// console.log(item)
			this.items = item;
		},
		methods: {
			onPressCancel() {
			    this.showConfirm = false;
			},
			async onPressSubmit() {
			    let url = this.ServicesApi.distribution;
			    let data = {
			    	id: this.items.id, //当前被分配的人才id
			    	user_id: this.item_list.id, //选择的管理员id
			    };
			    let result = await this.Services.post(url, data);
			    if (result.code == this.StatusCode.SUCCESS_CODE) {
			    	this.name = this.item_list.name;
					this.showConfirm = false;
			    	uni.showToast({
			    		icon: 'none',
						title: result.msg,
			    	});
			    } else {
			    	uni.showToast({
			    		icon: 'none',
			    		title: result.msg,
			    	});
			    }
			},
			// 取token
			getToken() {
				try {
					const value = uni.getStorageSync('login_key');
					if (value) {
						this.token = value;
					}
				} catch (e) {
					// error
				}
			},
			async reqList() {
				this.getToken()
				// 发请求
				let url = this.ServicesApi.userList;
				let data = {

				};
				let result = await this.Services.post(url, data);
				if (result.code == this.StatusCode.SUCCESS_CODE) {
					if (result.data[0] && result.data[0].id == '') {
						result.data.splice(0, 1);
					}
					this.nameList = result.data;
				} else {
					uni.showToast({
						icon: 'none',
						title: result.msg,
					});
				}
			},
			// 点击分配
			handleClickAllot(list) {
				this.item_list = list;
				this.showConfirm = true;
				this.item_content = "确认要将" + this.items.name + "分配给" + list.name + "吗？";
				return;
				// console.log(list)
				uni.showModal({
					title: '温馨提示',
					content: "确认要将" + this.items.name + "分配给" + list.name + "吗？",
					showCancel: true,
					cancelText: '取消',
					confirmText: '确认',
					success: async res => {
						if (res.confirm) {
							this.getToken()
							// 发请求
							let url = this.ServicesApi.distribution;
							let data = {
								id: this.items.id, //当前被分配的人才id
								user_id: list.id, //选择的管理员id
							};
							let result = await this.Services.post(url, data);
							if (result.code == this.StatusCode.SUCCESS_CODE) {
								this.name = list.name;
								uni.showToast({
									icon: 'none',
									title: result.msg,
								});
							} else {
								uni.showToast({
									icon: 'none',
									title: result.msg,
								});
							}
						} else if (res.cancel) {}
					}
				});
			}
		},
		mounted() {
			this.reqList();
		}
	}
</script>

<style scoped lang="stylus">
	.top-box {
		height: 142upx;
		background: #fff;
		padding: 0 30upx;
		margin-bottom: 20upx;
		align-items: center;

		.top-now {
			height: 42upx;
			width: 120upx;

			.img-sty {
				height: 42upx;
				width: 120upx;
			}
		}

		.top-name {
			font-size: 36upx;
			color: #333333;
			margin-left: 24upx;
		}

		.top-res {
			font-size: 30upx;
			color: #666666;
			margin-left: 24upx;

		}
	}

	.sty {
		justify-content space-between;
		align-items: center;
		border-bottom: 1upx solid #DBDBDB;
		padding: 40upx;
		background: #fff;
	}

	.btn {
		height: 54upx;
		width: 160upx;
		text-align: center;
		line-height: 54upx;
		color: #fff;
		background: #4A90E2;
	}
</style>
